#include "xscreen.h"
#include <iostream>
#include <QTime>
#include "XScreenRecord.h"
using namespace std;
static bool isRecord = false;

#define RECORDQSS "\
QPushButton:!hover \
{background-image: url(:/QtForOpenCV4Tool/bin/images/record_normal.png);}\
QPushButton:hover\
{background-image: url(:/QtForOpenCV4Tool/bin/images/record_hot.png);}\
QPushButton:pressed{\
background-image: url(:/QtForOpenCV4Tool/bin/images/record_pressed.png);\
background-color: rgba(255, 255, 255, 0);}"

static QTime rtime;


XScreen::XScreen(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	setWindowFlags(Qt::FramelessWindowHint);
	setAttribute(Qt::WA_TranslucentBackground);
	startTimer(100);

}
void XScreen::timerEvent(QTimerEvent *e)
{
	if (isRecord)
	{
		int es = rtime.elapsed() / 1000;
		char buf[1024] = { 0 };
		sprintf(buf, "%03d:%02d", es / 60, es % 60);
		ui.timelabel->setText(buf);
	}
}
void XScreen::Record()
{
	isRecord = !isRecord;
	cout << "Record";
	if (isRecord)
	{
		rtime.restart();
		ui.recordButton->setStyleSheet("background-image: url(:/QtForOpenCV4Tool/bin/images/stop.png);background-color: rgba(255, 255, 255, 0);");
		QDateTime t = QDateTime::currentDateTime();
		QString filename = t.toString("yyyyMMdd_hhmmss");
		filename = "xcreen_" + filename;
		filename += ".mp4";
		filename = ui.urlEdit->text() + "\\" + filename;
		XScreenRecord::Get()->outWidth = ui.widthEdit->text().toInt();
		XScreenRecord::Get()->outHeight = ui.heightEdit->text().toInt();
		XScreenRecord::Get()->fps = ui.fpsEdit->text().toInt();
		if (XScreenRecord::Get()->Start(filename.toLocal8Bit()))
		{
			return;
		}

		isRecord = false;
	}
	
	{
		ui.recordButton->setStyleSheet(RECORDQSS);
		XScreenRecord::Get()->Stop();
	}

}

void XScreen::mousePressEvent(QMouseEvent* event)
{
	if (event->button() == Qt::LeftButton) {
		isDragging = true;
		dragPosition = event->globalPos() - frameGeometry().topLeft();
		event->accept();
	}
}

void XScreen::mouseMoveEvent(QMouseEvent* event)
{
	if (isDragging && (event->buttons() & Qt::LeftButton)) {
		move(event->globalPos() - dragPosition);
		event->accept();
	}
}

void XScreen::mouseReleaseEvent(QMouseEvent* event)
{
	isDragging = false;
	event->accept();
}
